package com.hongliang.lock;

import java.util.concurrent.TimeUnit;

/**
 * 同步锁
 *
 * 同一个id才需要加同步锁操作，不同id不要
 *
 * @author Hongliang Zhu
 * @create 2022-02-27 19:32
 */
public class SyncLock {

    private SynchronizedByKey synchronizedByKey = new SynchronizedByKey();

    public void process(String id) {

        synchronizedByKey.exec(id, ()->{
            System.out.println("开始... "+ id);
            service();
            System.out.println("结束... "+ id);
        });
    }

    private void service() {

        try {
            TimeUnit.MILLISECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
